package com.ttl.bc.ap.ap.db;

import hibernate.util.HibernateUtil;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.ttl.bc.ap.ap.bean.BCAPBean;

import com.ttl.bc.ap.invoice.bean.BCAPInvoiceBean;

import com.ttl.common.db.ADAO;

public class BCAPDAO extends ADAO {

	@SuppressWarnings("unchecked")
	public boolean updateDebtAmount(BCAPInvoiceBean inv) {
		Session session = null;
		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			double tempAmount = 0;
			//เผื่อกรณีแก้ไข docNo อันนั้น คือให้ดึงมูลค่าใน invoice ใบนั้นออกมาก่อน แล้วไปหักออกจาก ยอดหนี้เดิม แล้วค่อยบวก ยอด invoiceใหม่เข้าไป
			// ปล. ต้องแก้ไขยอดหนี้ให้เรียบร้อยก่อน   ก่อนที่จะทำการ update invoice ที่แก้ไข (ควรให้การแก้ไข invoice ถูก save เป็นลำดับสุดท้าย)
			String hql ="FROM BCAPInvoiceBean WHERE docNo ='"+inv.getDocNo()+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            if (resultList!=null && resultList.size()>0) {		            
            	BCAPInvoiceBean h = (BCAPInvoiceBean)resultList.get(0);
            	tempAmount = h.getNetDebtAmount();
            }			
						
			hql ="FROM BCAPBean WHERE code ='"+inv.getApCode()+"'";
            q = session.createQuery(hql);
            resultList = q.list();
            
            if (resultList!=null && resultList.size()>0) {		            
            	BCAPBean h = (BCAPBean)resultList.get(0);
            	h.setDebtAmount(h.getDebtAmount()-tempAmount+inv.getNetDebtAmount());		        
            	session.update(h);
            	
            	System.out.println("h.getDebtAmount()="+h.getDebtAmount());
            	System.out.println("tempAmount="+tempAmount);
            	System.out.println("inv.getNetDebtAmount()="+inv.getNetDebtAmount());
            }
            			
			session.getTransaction().commit();
			
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		}finally {
			session.close();
		}		
	}
	
	@SuppressWarnings("unchecked")
	public List findAll() {
		List result = null;
		Session session = null;
		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery("FROM BCAPBean ORDER BY code");
			result = q.list();
			
			session.getTransaction().commit();						
			
		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		return result;
	}	
	
	@SuppressWarnings("unchecked")
	public List find(String code) {
		List result = null;
		Session session = null;
		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery("FROM BCAPBean WHERE code='"+code+"'");
			result = q.list();

//			if (result.size()>0) {
//				for (int i=0; i<result.size(); i++) {
//					BCAPBean a = (BCAPBean)result.get(i);
//					System.out.print("  "+a.getCode());
//					System.out.print("  "+new String(a.getName1().getBytes("ISO8859_1"),"UTF-8"));
//					System.out.println("  "+a.getName2());					
//				}			
//			}			
			
			session.getTransaction().commit();						
			
		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	public boolean updateDebtAmount(String apCode, Double sumOfTotalTax, String cndn) {
		Session session = null;
		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
						
						
			String hql ="FROM BCAPBean WHERE code ='"+apCode+"'";
			org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();
            
            if (resultList!=null && resultList.size()>0) {		            
            	BCAPBean h = (BCAPBean)resultList.get(0);
            	
            	
            	if(cndn.equalsIgnoreCase("CN")) {
            		h.setDebtAmount(h.getDebtAmount()-sumOfTotalTax);
            	}else if(cndn.equalsIgnoreCase("DN")) {
            		h.setDebtAmount(h.getDebtAmount()+sumOfTotalTax);
            	}
            	
            	session.save(h);
            	
            }
            			
			session.getTransaction().commit();

			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		}finally {
			session.close();
		}
		
	}	

}
